package {
	
	import org.cove.ape.*;
	
	public class Block extends Group {
		
		//Four blocks to make one big block
		private var rectangleParticleA:RectangleParticle;
		private var rectangleParticleB:RectangleParticle;
		private var rectangleParticleC:RectangleParticle;
		private var rectangleParticleD:RectangleParticle;
		
		//For collision detection at the bottom.
		//Might have to change later
		private var circleParticleB:CircleParticle;
		private var constraintC:SpringConstraint;
		
		//constraints between blocks.
		//Six needed total
		private var constraintD:SpringConstraint;
		private var constraintE:SpringConstraint;
		private var constraintF:SpringConstraint;
		
		private var constraintG:SpringConstraint;
		private var constraintH:SpringConstraint;
		private var constraintI:SpringConstraint;

		
		private var internalComp:Composite; // composite containing all the stuff so we can rotate it
		
		//vector forces to apply to the block with the move and
		//jump functions below
		private var moveForceLeft:Vector = new Vector(-50, 0);
		private var moveForceRight:Vector = new Vector(50,0)
		private var jumpForce:Vector = new Vector(0, -300);		
		
		private static var xVel:Number = 5;
		private static var jumpVel:Number = 20;
		
		
		public function Block(colA:uint) {
			internalComp = new Composite();
			
			
			rectangleParticleA = new RectangleParticle(320,110,15,15,0,false,4, 0.005, 0.05);
			rectangleParticleA.setStyle(0, colA, 1, colA);
			//addParticle(rectangleParticleA);
			internalComp.addParticle(rectangleParticleA);
			//rectangleParticleA.visible=false;
			rectangleParticleA.sprite.cacheAsBitmap = true;
			//rectangleParticleA.visible = false;
			rectangleParticleA.velocity = new Vector(0, 0);
			//rectangleParticleA.sprite.name = "footParticle";
			
			rectangleParticleB = new RectangleParticle(380,110,15,15,0,false,4, 0.005, 0.05);
			rectangleParticleB.setStyle(0, colA, 1, colA);
			//addParticle(rectangleParticleB);
			internalComp.addParticle(rectangleParticleB);
			rectangleParticleB.sprite.cacheAsBitmap = true;
			rectangleParticleB.velocity = new Vector(0, 0);
			
			rectangleParticleC = new RectangleParticle(320,170,15,15,0,false,4, 0.005, 0.05);
			rectangleParticleC.setStyle(0, colA, 1, colA);
			//addParticle(rectangleParticleB);
			internalComp.addParticle(rectangleParticleC);
			rectangleParticleC.sprite.cacheAsBitmap = true;
			rectangleParticleC.velocity = new Vector(0, 0);
			
			rectangleParticleD = new RectangleParticle(380,170,15,15,0,false,4, 0.005, 0.05);
			rectangleParticleD.setStyle(0, colA, 1, colA);
			//addParticle(rectangleParticleB);
			internalComp.addParticle(rectangleParticleD);
			rectangleParticleD.sprite.cacheAsBitmap = true;
			rectangleParticleD.velocity = new Vector(0, 0);
			
			//Spring constraints
			
			//From Block A....
			constraintD = new SpringConstraint(rectangleParticleA,rectangleParticleB,0.5,true,1,1,true)
			constraintD.visible = false;
			internalComp.addConstraint(constraintD);
			
			constraintE = new SpringConstraint(rectangleParticleA,rectangleParticleC,0.5,true,1,1,true)
			constraintE.visible = false;
			internalComp.addConstraint(constraintE);
			
			constraintF = new SpringConstraint(rectangleParticleA,rectangleParticleD,0.5,true,1,1,true)
			constraintF.visible = false;
			internalComp.addConstraint(constraintF);
			
			//From Block B...
			constraintG = new SpringConstraint(rectangleParticleB,rectangleParticleC,0.5,true,1,1,true)
			constraintG.visible = false;
			internalComp.addConstraint(constraintG);
			
			constraintH = new SpringConstraint(rectangleParticleB,rectangleParticleD,0.5,true,1,1,true)
			constraintH.visible = false;
			internalComp.addConstraint(constraintH);
			
			//From Block C...
			constraintI = new SpringConstraint(rectangleParticleC,rectangleParticleD,0.5,true,1,1,true)
			constraintI.visible = false;
			internalComp.addConstraint(constraintI);
			
			
			//Not yet invisible circle to test for collision against floor
			circleParticleB = new CircleParticle(320,140,0.5,false,0.5,0.1,0.05);
			circleParticleB.setStyle(0, colA, 1, 0xffff00);
			circleParticleB.visible = true;
			circleParticleB.sprite.name = "footParticle";
			internalComp.addParticle(circleParticleB);
			
			//Spring constraint to keep block and circle together.
			constraintC = new SpringConstraint(rectangleParticleA,circleParticleB,1,false);
			constraintC.visible = false;
			internalComp.addConstraint(constraintC); 
			
			addComposite(internalComp);
		}		

		
		//make the block move left 
		public function moveLeft():void{	
			//rectangleParticleA.addForce(moveForceLeft);
			rectangleParticleA.velocity = new Vector(-xVel, 0);	
			rectangleParticleB.velocity = new Vector(-xVel, 0);
			rectangleParticleC.velocity = new Vector(-xVel, 0);
			rectangleParticleD.velocity = new Vector(-xVel, 0);
		}
		
		//make the block move right 
		public function moveRight():void{	
			//rectangleParticleA.addForce(moveForceRight);
			rectangleParticleA.velocity = new Vector(xVel, 0);	
			rectangleParticleB.velocity = new Vector(xVel, 0);
			rectangleParticleC.velocity = new Vector(xVel, 0);
			rectangleParticleD.velocity = new Vector(xVel, 0);
		}
		
		//make the block jump 
		public function jump():void{
			//rectangleParticleA.addForce(jumpForce);
			rectangleParticleA.velocity = new Vector(rectangleParticleA.velocity.x, -jumpVel);
			rectangleParticleB.velocity = new Vector(rectangleParticleA.velocity.x, -jumpVel);
			rectangleParticleC.velocity = new Vector(rectangleParticleA.velocity.x, -jumpVel);
			rectangleParticleD.velocity = new Vector(rectangleParticleA.velocity.x, -jumpVel);
		}
		
		
		// function to rotate internalComp - gravity polarizing!
		public function rotate(degrees:Number, centerX:Number, centerY:Number):void {
			// store velocity info for later rotation -- velocity info is deleted when you rotate by angle.
			//trace("yvel init: "+ rectangleParticleA.velocity.y);
			var Xi:Number = rectangleParticleA.velocity.x;
			var Yi:Number = rectangleParticleA.velocity.y;
			
			// rotate position
			internalComp.rotateByAngle(degrees, new Vector(centerX, centerY));
			
			var radAngle:Number = degrees * Math.PI/180; // convert to radians
			
			rectangleParticleA.velocity = new Vector(Xi*Math.cos(radAngle) - Yi*Math.sin(radAngle), 
								Xi*Math.sin(radAngle) + Yi*Math.cos(radAngle) );
			
			//trace("yvel final: "+ rectangleParticleA.velocity.y);
			
			
			/*
			var radAngle:Number = degrees * Math.PI/180; // convert to radians
			for (var i=0; i<internalComp.particles.length; i++) {
				internalComp.particles[i].radian += radAngle;
				internalComp.particles[i].paint();
			}*/
		}
	}
			
}

